package com.example.idachuappone.utils;

import java.math.BigDecimal;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;

import com.lidroid.xutils.BitmapUtils;
import com.lidroid.xutils.bitmap.BitmapGlobalConfig.BitmapCacheManagementTask;

public class BitmapUtilHelper {
	private static BitmapUtils bitmapUtils;

	public static synchronized BitmapUtils createBitmapUtil(Context context) {
		BitmapUtils bitmapUtils = null;
		synchronized (BitmapUtilHelper.class) {
			if (bitmapUtils == null) {
				bitmapUtils = new BitmapUtils(context, FileUtil.getBitmapUtilCacheDir(), 4194304, 31457280);
			}
			bitmapUtils = bitmapUtils;
		}
		return bitmapUtils;
	}

	public static Bitmap reduce(Bitmap bitmap, int width, int height, boolean isAdjust) {
		if (bitmap.getWidth() < width && bitmap.getHeight() < height) {
			return bitmap;
		}
		float sx = new BigDecimal(width).divide(new BigDecimal(bitmap.getWidth()),
				BitmapCacheManagementTask.MESSAGE_CLEAR, 1).floatValue();
		float sy = new BigDecimal(height).divide(new BigDecimal(bitmap.getHeight()),
				BitmapCacheManagementTask.MESSAGE_CLEAR, 1).floatValue();
		if (isAdjust) {
			if (sx >= sy) {
				sx = sy;
			}
			sy = sx;
		}
		Matrix matrix = new Matrix();
		matrix.postScale(sx, sy);
		return Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
	}

	public static Bitmap getCroppedRoundBitmap(Bitmap bmp, int radius) {
		Bitmap squareBitmap;
		Bitmap scaledSrcBmp;
		int diameter = radius * 2;
		int bmpWidth = bmp.getWidth();
		int bmpHeight = bmp.getHeight();
		Bitmap bitmap;
		int i;
		if (bmpHeight > bmpWidth) {
			bitmap = bmp;
			i = (bmpHeight - bmpWidth) / 2;
			squareBitmap = Bitmap.createBitmap(bitmap, 0, i, bmpWidth, bmpWidth);
		} else if (bmpHeight < bmpWidth) {
			int x = (bmpWidth - bmpHeight) / 2;
			bitmap = bmp;
			i = 0;
			squareBitmap = Bitmap.createBitmap(bitmap, x, i, bmpHeight, bmpHeight);
		} else {
			squareBitmap = bmp;
		}
		if (squareBitmap.getWidth() == diameter && squareBitmap.getHeight() == diameter) {
			scaledSrcBmp = squareBitmap;
		} else {
			scaledSrcBmp = Bitmap.createScaledBitmap(squareBitmap, diameter, diameter, true);
		}
		Bitmap output = Bitmap.createBitmap(scaledSrcBmp.getWidth(), scaledSrcBmp.getHeight(), Config.ARGB_8888);
		Canvas canvas = new Canvas(output);
		Paint paint = new Paint();
		Rect rect = new Rect(0, 0, scaledSrcBmp.getWidth(), scaledSrcBmp.getHeight());
		paint.setAntiAlias(true);
		paint.setFilterBitmap(true);
		paint.setDither(true);
		canvas.drawARGB(0, 0, 0, 0);
		canvas.drawCircle(scaledSrcBmp.getWidth() / 2, scaledSrcBmp.getHeight() / 2, scaledSrcBmp.getWidth() / 2, paint);
		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(scaledSrcBmp, rect, rect, paint);
		return output;
	}

	public static Bitmap toRoundCorner(Bitmap bitmap, int pixels) {
		Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
		Canvas canvas = new Canvas(output);
		Paint paint = new Paint();
		Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
		RectF rectF = new RectF(rect);
		float roundPx = pixels;
		paint.setAntiAlias(true);
		canvas.drawARGB(0, 0, 0, 0);
		paint.setColor(-12434878);
		canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
		paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
		canvas.drawBitmap(bitmap, rect, rect, paint);
		return output;
	}
}
